﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.User
{
	/// <summary>
	/// 用户控制类
	/// </summary>
	/// <![CDATA[2014-7-30 09:14:12]]>
	public class Component : ComponentBase
	{
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-7-30 09:14:12]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-7-30 09:14:12]]>
		private Config clsConfig = new Config();		// 配置类对象

		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strUsername", clsDatabase.USERNAME, clsDatabase.listStructure[clsDatabase.USERNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPassword", clsDatabase.PASSWORD, clsDatabase.listStructure[clsDatabase.PASSWORD].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nUserGroupID", clsDatabase.USERGROUPID, clsDatabase.listStructure[clsDatabase.USERGROUPID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strColumnIDs", 0, clsDatabase.clsUserGroupDB.listStructure[clsDatabase.clsUserGroupDB.COLUMNIDS].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strUserGroupName", 0, clsDatabase.clsUserGroupDB.listStructure[clsDatabase.clsUserGroupDB.USERGROUPNAME].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nDepartmentID", clsDatabase.DEPARTMENTID, clsDatabase.listStructure[clsDatabase.DEPARTMENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDepartmentName", 0, clsDatabase.clsDepartmentDB.listStructure[clsDatabase.clsDepartmentDB.NAME].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strWebColumnIDs", 0, clsDatabase.clsUserGroupDB.listStructure[clsDatabase.clsUserGroupDB.WEBCOLUMNIDS].strName, false));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonID", clsDatabase.PERSONID, clsDatabase.listStructure[clsDatabase.PERSONID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strJobNumber", clsDatabase.JOBNUMBER, clsDatabase.listStructure[clsDatabase.JOBNUMBER].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strPersonName", clsDatabase.PERSONNAME, clsDatabase.listStructure[clsDatabase.PERSONNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strIDCard", clsDatabase.IDCARD, clsDatabase.listStructure[clsDatabase.IDCARD].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nRegister", clsDatabase.REGISTER, clsDatabase.listStructure[clsDatabase.REGISTER].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strUsername", clsDatabase.listStructure[clsDatabase.USERNAME].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strPassword", clsDatabase.listStructure[clsDatabase.PASSWORD].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strLikeUsername", clsDatabase.listStructure[clsDatabase.USERNAME].strName, "LIKE"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nUserGroupID", clsDatabase.listStructure[clsDatabase.USERGROUPID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strIDCard", clsDatabase.listStructure[clsDatabase.IDCARD].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strJobNumber", clsDatabase.listStructure[clsDatabase.JOBNUMBER].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strPersonName", clsDatabase.listStructure[clsDatabase.PERSONNAME].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strPersonID", clsDatabase.listStructure[clsDatabase.PERSONID].strName, "="));
		}

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-7-30 09:14:12]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 操作
		/// <summary>
		/// 根据唯一吗、工号、姓名和身份证由系统自动注册一个用户
		/// </summary>
		/// <param name="strPersonID">唯一吗</param>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strPersonName">姓名</param>
		/// <param name="strIDCard">身份证</param>
		/// <param name="clsUser">新添加用户的实体对象</param>
		/// <returns>是否成功添加</returns>
		public bool Add(string strPersonID, string strJobNumber, string strPersonName, string strIDCard, ref Entity clsUser)
		{
			Entity clsAddUser = new Entity();

			clsAddUser.Username = Guid.NewGuid().ToString();
			clsAddUser.Password = clsConfig.strInitPassword;
			clsAddUser.UserGroupID = clsConfig.nLearnerUserGroupID;
			clsAddUser.PersonID = strPersonID;
			clsAddUser.JobNumber = strJobNumber;
			clsAddUser.PersonName = strPersonName;
			clsAddUser.IDCard = strIDCard;
			clsAddUser.Register = false;

			if (Add(clsAddUser))
			{
				clsUser = QueryByPersonID(strPersonID);

				return true;
			}
			else
			{
				clsUser = null;

				return false;
			}

		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-10 16:30:43]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Add(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsUser = new Entity();
			
			clsUser = (Entity)clsEntity;

			if (clsUser.UserGroupID == clsConfig.nLearnerUserGroupID)
			{
				return AddLearnUser(clsUser);
			}
			else
			{
				clsUser.Register = false;
				clsUser.Password = clsConfig.strInitPassword;

				return base.Add(clsUser);
			}

		}

		private bool AddLearnUser(Entity clsUser)
		{
			Person.Component clsPersonComp = new Person.Component();
			Person.Entity clsPerson = new Person.Entity();
			Entity clsModify = new Entity();


			// 检查注册时的工号、姓名、身份证号是否在人力库中存在，并唯一
			clsPerson = clsPersonComp.Get(clsUser.JobNumber, clsUser.PersonName, clsUser.IDCard);
			if (clsPerson != null)
			{
				clsUser.PersonID = clsPerson.usysid;

				if (HaveUsername(clsUser.Username))
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "用户名已经存在！");
					return false;
				}
				else
				{
					if (clsUser.IDCard != "")
					{
						clsModify = QueryByIDCard(clsUser.IDCard);
					}
					else
					{
						clsModify = null;
					}

					if (clsModify != null)
					{
						if (!clsModify.Register)
						{
							// 修改
							clsModify.Username = clsUser.Username;
							clsModify.Password = clsUser.Password;
							clsModify.DepartmentID = clsUser.DepartmentID;
							clsModify.Register = true;

							return Modify(clsModify);
						}
						else
						{
							TonyNet4V1.ShowMessageAlert.ShowMessage(page, "您已经注册过了！请联系管理员找回用户名密码");
							return false;
						}
					}
					else
					{
						if (clsUser.Password == "")
						{
							clsUser.Password = clsConfig.strInitPassword;
						}

						return base.Add(clsUser);
					}
				}
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(page, "人员信息错，注册失败！");
				return false;
			}
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <![CDATA[2014-8-10 16:36:34]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Modify(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsOldUser = new Entity();
			Entity clsNewUser = new Entity();

			clsNewUser = (Entity)clsEntity;
			clsOldUser = GetOne(clsNewUser.ID);
			if (clsNewUser.Username == clsOldUser.Username)
			{
				return base.Modify(clsEntity);
			}
			else
			{
				if (HaveUsername(clsNewUser.Username))
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "用户名已经存在！");
					return false;
				}
				else
				{
					return base.Modify(clsEntity);
				}
			}
			
		}

		/// <summary>
		/// 修改用户名
		/// </summary>
		/// <param name="strNewUsername">新用户名</param>
		/// <param name="nUserID">用户ID</param>
		/// <returns></returns>
		public bool ModifyUsername(int nUserID, string strNewUsername)
		{
			Entity clsUser = new Entity();
			clsUser = GetOne(nUserID);
			if (clsUser.Username == strNewUsername)
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(page, "要修改的用户名和当前用户名一样");
				return false;
			}
			else
			{
				QueryEntity clsQuery = new QueryEntity();

				clsQuery.strUsername = strNewUsername;
				if (Query(clsQuery).Count > 0)
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "新用户名已经存在！");
					return false;
				}
				else
				{
					clsUser.Username = strNewUsername;
					return Modify(clsUser);
				}
			}
		}
		/// <summary>
		/// 修改密码
		/// </summary>
		/// <param name="nUserID">用户ID</param>
		/// <param name="strOldPassword">老密码</param>
		/// <param name="strNewPassword1">新密码</param>
		/// <param name="strNewPassword2">确认密码</param>
		/// <returns></returns>
		public bool ModifyPassword(int nUserID, string strOldPassword, string strNewPassword1, string strNewPassword2)
		{
			if (strNewPassword1 == strNewPassword2)
			{
				Entity clsUser = new Entity();

				clsUser = GetOne(nUserID);
				if (clsUser.Password == strOldPassword)
				{
					clsUser.Password = strNewPassword1;
					return Modify(clsUser);
				}
				else
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(page, "密码输入错误！");
					return false;
				}
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(page, "两次新密码不一致！");
				return false;
			}
		}

		/// <summary>
		/// 初始化一个用户密码
		/// </summary>
		/// <param name="nUserID">用户ID</param>
		/// <returns></returns>
		public bool InitPassword(int nUserID)
		{
			Entity clsUser = new Entity();

			clsUser = GetOne(nUserID);

			clsUser.Password = clsConfig.strInitPassword;

			return Modify(clsUser);
		}

		#endregion 操作
		#region 查询

		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-7-30 09:14:12]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-7-30 09:14:12]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-7-30 11:45:54]]>
		/// <param name="clsQuery"></param>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询

		/// <summary>
		/// 根据身份证查找
		/// </summary>
		/// <param name="strIDCard"></param>
		/// <returns></returns>
		public Entity QueryByIDCard(string strIDCard)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strIDCard = strIDCard;
			list = Query(clsQuery);
			if (list.Count > 0)
			{
				return list[0];
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 根据唯一吗查询
		/// </summary>
		/// <param name="strPersonID">唯一吗</param>
		/// <returns>用户实体对象，没有查到返回null</returns>
		public Entity QueryByPersonID(string strPersonID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strPersonID = strPersonID;

			list = Query(clsQuery);

			if (list.Count > 0)
			{
				return list[0];
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 检查用户是否有权登录系统
		/// </summary>
		/// <![CDATA[2014-7-30 11:39:41]]>
		/// <param name="strUsername">用户名</param>
		/// <param name="strPassword">密码</param>
		/// <param name="clsLoginUser">有权登录的用户实体对象</param>
		/// <returns>成功登录返回true，否则返回false</returns>
		public bool CheckLogin(string strUsername, string strPassword, ref Entity clsLoginUser)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strUsername = strUsername;
			clsQuery.strPassword = strPassword;

			list = Query(clsQuery);

			if (list.Count > 0)
			{
				clsLoginUser = list[0];

				return true;
			}
			else
			{
				clsLoginUser = null;
				return false;
			}
		}

		/// <summary>
		/// 检查用户是否已经存在
		/// </summary>
		/// <![CDATA[2014-8-10 16:29:17]]>
		/// <param name="strUsername"></param>
		/// <returns></returns>
		private bool HaveUsername(string strUsername)
		{
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.strUsername = strUsername;

			if (Query(clsQuery).Count > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 检查没有用户名，输入工号、姓名、身份证登录用户，并根据培训班ID检查该登录用户是否报名
		/// </summary>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strPersonName">姓名</param>
		/// <param name="strIDCard">身份证</param>
		/// <param name="clsUser">成功登录用户实体</param>
		/// <param name="nTrainingID">培训班ID</param>
		/// <returns>登录成功返回真，否则返回假</returns>
		public bool CheckNoUserLogin(string strJobNumber, string strPersonName, string strIDCard, ref Entity clsUser, int nTrainingID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			TrainingPerson.Component clsTrainingPersonComp = new TrainingPerson.Component(page);

			clsQuery.strJobNumber = strJobNumber;
			clsQuery.strPersonName = strPersonName;
			clsQuery.strIDCard = strIDCard;

			list = Query(clsQuery);
			if (list.Count > 0)
			{
				clsUser = list[0];
				if (clsTrainingPersonComp.CheckSignUpInfo(strJobNumber, strPersonName, strIDCard, nTrainingID) != null)
				{
					clsUser = list[0];
					return true;
				}
				else
				{
					clsUser = null;
					return false;
				}
			}
			else
			{
				clsUser = null;
				return false;
			}
		}

		/// <summary>
		/// 检查没有用户名，输入工号、姓名、身份证登录用户
		/// </summary>
		/// <param name="strJobNumber">工号</param>
		/// <param name="strPersonName">姓名</param>
		/// <param name="strIDCard">身份证</param>
		/// <param name="clsUser">成功登录用户实体</param>
		/// <returns>登录成功返回真，否则返回假</returns>
		public bool CheckNoUserLogin(string strJobNumber, string strPersonName, string strIDCard, ref Entity clsUser)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			TrainingPerson.Component clsTrainingPersonComp = new TrainingPerson.Component(page);

			clsQuery.strJobNumber = strJobNumber;
			clsQuery.strPersonName = strPersonName;
			clsQuery.strIDCard = strIDCard;

			list = Query(clsQuery);
			if (list.Count > 0)
			{
				clsUser = list[0];
				return true;
			}
			else
			{
				clsUser = null;
				return false;
			}
		}
	}
}
